package com.helloae.chap06;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import com.esri.arcgis.geodatabase.FeatureClassName;
import com.esri.arcgis.geodatabase.FeatureDataConverter;
import com.esri.arcgis.geodatabase.FeatureDatasetName;
import com.esri.arcgis.geodatabase.WorkspaceName;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 新建一个Personal Geodatabase后，将指定文件夹(不包括子文件夹)下的所有shp文件导入同一个FeatureDataset中。<br>
 * 这个例子与6.3.2的MultipleShapefilesToAccess非常像，仅仅只是convertFeatureClass执行时多了一个FeatureDatasetName，其他完全相同<br>
 * 示例章节：6.3.3
 * @author zhushijie
 *
 */
public class ShapefilesToFeatureDataset {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
		
		//创建用于输出的IFeatureClassName并配置其所需的参数
		WorkspaceName pWorkspaceName = new WorkspaceName();
		pWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesGDB.AccessWorkspaceFactory");
		pWorkspaceName.setPathName("c:\\temp\\Trial2.mdb");
		FeatureClassName pFeatureClassName = new FeatureClassName();
		pFeatureClassName.setWorkspaceNameByRef(pWorkspaceName);
		
		//创建用于输入的IFeatureClassName并配置其所需的参数
		WorkspaceName pInShpWorkspaceName = new WorkspaceName();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		String inputPath = ecliseWorkspace+"HelloAE"+File.separator+"DataSet"+File.separator+"chap06";
		pInShpWorkspaceName.setPathName(inputPath);
		pInShpWorkspaceName.setWorkspaceFactoryProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
		FeatureClassName pShpDatasetName = new FeatureClassName();
		pShpDatasetName.setWorkspaceNameByRef(pInShpWorkspaceName);
		
		FeatureDataConverter pShpToFC = new FeatureDataConverter();
		
		//**************************************************************************************
		//		用于存放数据的FeatureDataset，只有这里和convertFeatureClass()与6.3.2不同
		//**************************************************************************************
		FeatureDatasetName pFeatDSName = new FeatureDatasetName();
		pFeatDSName.setWorkspaceNameByRef(pWorkspaceName);
		pFeatDSName.setName("StudyArea1");
		
		
		//设置输入文件夹
		File inputDir = new File(inputPath);
		File[] files = inputDir.listFiles();
		
		//遍历输入文件夹，将shp文件全部插入Personal Geodatabase中
		for(int i = 0; i < files.length; i++){
			if(files[i].isFile() && isShp(files[i].getName())){
				//获得没有后缀名的文件名，如“river.shp”就获得“river”
				String preName = files[i].getName().substring(0, files[i].getName().length()-4);
				pFeatureClassName.setName(preName);
				pShpDatasetName.setName(preName);
				
				//插入Personal Geodatabase。
				//此过程会创建Personal Geodatabase再执行插入，
				//如果Personal Geodatabase已存在则直接插入，如果库内已有同名文件则报错。
				pShpToFC.convertFeatureClass(pShpDatasetName, null, pFeatDSName, pFeatureClassName, null, null, "", 1000, 0);
				System.out.println(files[i].getName() + "已入库。");
			}
		}
		
		ao.shutdown();
		System.out.println("Congratulations!");
	}

	//简单判断文件后缀名是否为shp
	public static boolean isShp(String fileName){
		if(fileName.toLowerCase().endsWith(".shp")){
			return true;
		}else {
			return false;
		}
	}
}
